package com.yao.ro.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.ro.entity.dto.EquipmentListDto;
import com.yao.ro.entity.vo.EquipmentListVo;
import com.yao.ro.service.EquipmentListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 装备材料表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-22
 */
@Api(tags = "装备材料管理")
@RestController
@RequestMapping(value = "/equipmentList")
@Slf4j
public class EquipmentListController {

    @Autowired
    private EquipmentListService equipmentListService;

    @ApiOperation(value = "新增装备材料", notes = "新增装备材料")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody EquipmentListDto model) {
        ValidateHelper.validateNull(model, new String[]{"needs", "goodsId", "collectStatus"});

        equipmentListService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改装备材料", notes = "修改装备材料")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody EquipmentListDto model) {
        return ApiResponse.success();
    }

    @ApiOperation(value = "删除装备材料", notes = "逻辑删除装备材料")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {
        return ApiResponse.success();
    }

    @ApiOperation(value = "详情", notes = "详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> get(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(equipmentListService.byId(id));
    }

    @ApiOperation(value = "装备清单列表(分页)", notes = "装备清单列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<EquipmentListVo>> list(@RequestBody PageQueryBean<EquipmentListDto> queryBean) {

        if (Objects.isNull(queryBean.getData())) {
            queryBean.setData(new EquipmentListDto());
        }

        return ApiResponse.success(equipmentListService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize()), queryBean.getData()));
    }


    @ApiOperation(value = "修改剩余材料数量", notes = "修改剩余材料数量")
    @RequestMapping(value = "/updateRemaining", method = RequestMethod.POST)
    public ApiResponse<Object> updateRemaining(@RequestBody EquipmentListDto model) {

        ValidateHelper.validateNull(model, new String[]{"id", "remaining"});

        equipmentListService.updateRemaining(model);

        return ApiResponse.success();
    }


    @ApiOperation(value = "修改价格", notes = "修改价格")
    @RequestMapping(value = "/updatePrice", method = RequestMethod.POST)
    public ApiResponse<Object> updatePrice(@RequestBody EquipmentListDto model) {

        ValidateHelper.validateNull(model, new String[]{"id", "price"});

        equipmentListService.updatePrice(model);

        return ApiResponse.success();
    }

}